﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections;



using System.IO;
using System.Net;
using System.Net.Cache;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;


namespace LCWMS
{
    public partial class  w_so062 : DockContent
    {
        DataSet dscustomer;
        DataSet ds_tran;
        Cursor currentCursor = Cursor.Current;

        public w_so062()
        {
            InitializeComponent();
            dgv_tran.AutoGenerateColumns = false;
        }

        private static int iJS = 0;
        private static int iJSset = 300;

        private void btn_Query_Click(object sender, EventArgs e)
        {
            //querydata();
            querydata_wms();
        }

        private void w_inv002_Load(object sender, EventArgs e)
        {
            tb_number.Text = "5";
            //dscustomer = new DataSet();
            //try
            //{
            //    dscustomer = new DataSet();
            //    dscustomer = main.g_class.comm_get_consignor();
            //    cb_consignor.DataSource = dscustomer.Tables[0];
            //    cb_consignor.ValueMember = "consignor";
            //    cb_consignor.DisplayMember = "consignor";

            //    //语言转换
            //    main.g_class.comm_changeControl(this, main.g_win_dataset);
            //}
            //catch (System.Exception ee)
            //{
            //    MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of transaction type", main.g_info_dataset) + "!" + ee.ToString());
            //    return;
            //}
           
            //库区
            //Loadwarehouse();
           
        }

        private void cb_consignor_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cb_consignor.Text.Trim();
            tb_cusname.Text = main.g_class.comm_findname(str_cust, dscustomer);
        }

        private void btn_excel_Click(object sender, EventArgs e)
        {
            if (dgv_tran.Rows.Count > 0)
            {
                main.g_class.DataGridViewToExcel(dgv_tran);
            }
        }

        //库区
        private void Loadwarehouse()
        {
            try
            {
                string str_sql;
                DataSet ds_line;
                //列出属于该波次的列表
                str_sql = "select distinct warehouse,(warehouse + '-' + warehouse_name) warehouse_name  from  " + main.g_space + ".mst_warehouse  order by warehouse ";
                if (main.dbtype == "SQL")
                    ds_line = main.dbservice.Sql_exec_selectsql(str_sql);
                else
                    ds_line = main.dbservice.oracle_exec_selectsql(str_sql);

                DataRow drnew;
                drnew = ds_line.Tables[0].NewRow();
                drnew["warehouse"] = "";
                drnew["warehouse_name"] = "";
                ds_line.Tables[0].Rows.InsertAt(drnew, 0);  //头上

                cbx_area.ValueMember = "warehouse";
                cbx_area.DisplayMember = "warehouse_name";

                cbx_area.DataSource = ds_line.Tables[0];
                if (cbx_area.Items.Count > 1)
                {
                    cbx_area.Text = "";
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show("线路加载失败!" + ee.ToString());
            }
        }

        //只能输入数据
        private void tb_number_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 检查是否按下了退格键
            if (e.KeyChar == (char)Keys.Back)
            {
                e.Handled = false;
            }
            else
            {
                // 检查输入的字符是否是数字（0-9）
                char numberChar = e.KeyChar;
                if (char.IsDigit(numberChar))
                {
                    // 输入的是数字，不进行处理
                    e.Handled = false;
                }
                else
                {
                    // 输入的不是数字，阻止输入
                    e.Handled = true;
                }
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                this.timer1.Enabled = true;
                if (tb_number.Text.Trim() == "" || tb_number.Text.Trim() == "0")
                {
                    tb_number.Text = "5";
                }
                iJS = 0;
                iJSset = int.Parse( tb_number.Text.Trim()) * 60;
               
                this.timer1.Enabled = true;
                tb_number.Enabled = false;
                btn_Query.Enabled = false;
            }
            else {
                iJS = 0;
                this.timer1.Enabled = false;
                tb_number.Enabled = true;
                btn_Query.Enabled = true; 
                label1.Text = "";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (iJS < iJSset) {
                iJS += 1;
            }else{
                iJS = 0;
                //刷新数据
                querydata();
            }
            label1.Text = iJS.ToString();
        }

        private void querydata() {

            //状态字段
            //warehouse             --库区
            //warehouse_name        --库区名
            //totalBillNumbers      --总单据数
            //endTotalBillNumbers   --已完成单据数
            //totalSKU              --总SKU数
            //totalBreakDownSKU     --总拆零商品数
            //totalCatonSku         --总整件商品数
            //totalBreakQty         --总拆零数
            //totalCatonQty         --总整件数
            //pickendtime           --拣货结束时间
            //pickstarttime         --拣货开始时间
            //totalBoxQty           --总周转箱数

            //刷新数据
            Cursor.Current = Cursors.WaitCursor;

            string ret = GetDeptFunction();

            string str_sql = "select outdept as warehouse,(select warehouse_name from " + main.g_space + ".mst_warehouse where inv_org='01.01.002.01' and warehouse=outdept) as warehouse_name," +
                "(case pickMode when '0' then '拆零' else '整件' end) as pickMode,totalBillNumbers,endTotalBillNumbers," +
                "(case when isnull(totalBillNumbers,0)>0 then CONVERT(varchar, CAST(isnull(endTotalBillNumbers,0) * 100.0 / totalBillNumbers AS DECIMAL(13, 2))) + '%' else '0.00%'end) AS bill_per,"+
                 "totalSKU,totalBreakDownSKU,totalCatonSku,totalBreakQty, totalCatonQty,pickendtime,pickstarttime,totalBoxQty," +
                "(case when DATEDIFF(HOUR, pickstarttime,pickendtime)>0  then isnull(endTotalBillNumbers,0) / DATEDIFF(HOUR, pickstarttime,pickendtime) else 0 end) HOURBill " +
                "from " + main.g_space + ".in_getDeptPickStatus where 1=1 ";
            str_sql += "order by outdept ";

            try
            {
                if (main.dbtype == "SQL")
                {
                    ds_tran = main.dbservice.Sql_exec_selectsql(str_sql);
                }
                else
                {
                    ds_tran = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                if (ds_tran.Tables[0].Rows.Count > 0)
                    dgv_tran.DataSource = ds_tran.Tables[0];
                else
                    dgv_tran.DataSource = null;
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to Query", main.g_info_dataset) + "!" + ee.ToString());
            }

            Cursor.Current = currentCursor;
        
        }

        private void querydata_wms()
        {

            //状态字段
            //warehouse             --库区
            //warehouse_name        --库区名
            //totalBillNumbers      --总单据数
            //endTotalBillNumbers   --已完成单据数
            //totalSKU              --总SKU数
            //totalBreakDownSKU     --总拆零商品数
            //totalCatonSku         --总整件商品数
            //totalBreakQty         --总拆零数
            //totalCatonQty         --总整件数
            //pickendtime           --拣货结束时间
            //pickstarttime         --拣货开始时间
            //totalBoxQty           --总周转箱数

            //刷新数据
            Cursor.Current = Cursors.WaitCursor;

            //string ret = GetDeptFunction();


            string str_sql = "select  outdept AS warehouse, warehouse_name, pickmode AS pickMode, " +

                 "  billnum   as totalBillNumbers , " +
                 "  f_billnum as endTotalBillNumbers," +
                 "    (" +
                 "    CASE" +
                 "" +
                 "        WHEN isnull(billnum, 0) > 0 THEN " +
                 "        CONVERT (VARCHAR, CAST (isnull(f_billnum, 0) * 100.0 / billnum AS DECIMAL (13, 2))) + '%' ELSE '0.00%'  " +
                 "      END) AS bill_per, " +
                 "  skunum        as totalSKU,           " + //-- 总SKU数 
                 "  parts_num     as totalBreakDownSKU,  " + // -- 总拆零商品数 
                 "  whole_num     as totalCatonSku,      " + // -- 总整件商品数 
                 "  parts_pickqty as totalBreakQty,      " + // -- 总拆零数 
                 "  whole_pickqty as totalCatonQty,      " + // -- 总整件数 
                 "  turnoverboxnum as totalBoxQty,       " + // -- 周转箱数 
                 " " +
                 "  pickendtime, " +
                 "  pickstarttime, " +
                 " ( CASE WHEN DATEDIFF(HOUR, pickstarttime, pickendtime) > 0 THEN isnull(f_billnum, 0) / DATEDIFF(HOUR, pickstarttime, pickendtime) ELSE 0  END) HOURBill  " +

                 " from " + main.g_space + ".VIEW_SQL_MONITOR_PICKBILL_WAREHOUSE t1 " +
                 " where 1=1 ";


            str_sql += "order by outdept ";

            try
            {
                if (main.dbtype == "SQL")
                {
                    ds_tran = main.dbservice.Sql_exec_selectsql(str_sql);
                }
                else
                {
                    ds_tran = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                if (ds_tran.Tables[0].Rows.Count > 0)
                    dgv_tran.DataSource = ds_tran.Tables[0];
                else
                    dgv_tran.DataSource = null;
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to Query", main.g_info_dataset) + "!" + ee.ToString());
            }

            Cursor.Current = currentCursor;

        }

        public string GetDeptFunction()
        {
            // 接口已废弃不用
            //try
            //{
            //    string[] sUrlArr = main.g_server.Split(':');
            //    if (sUrlArr.Length == 0)
            //        return "";

            //    string sUrl = "http://" + sUrlArr[0] + ":8057/api/getDeptPickStatus";
            //    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
            //    request.Method = "GET";
            //    request.ContentType = "text/html;charset=UTF-8";
            //    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //    Stream myResponseStream = response.GetResponseStream();
            //    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            //    string retString = myStreamReader.ReadToEnd();
            //    myStreamReader.Close();
            //    myResponseStream.Close();
            //    return retString;

            //}
            //catch (Exception ex)
            //{
            //    return "";
            //}

            return "";
        }

        private void dgv_tran_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dgv_tran.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dgv_tran.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dgv_tran.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

    }
}